package cn.leo.dynamic_multi_datasource.datasource;

import java.util.ArrayList;
import java.util.List;

/**
 * @author leo.z.l
 * @create 2019-09-06  16:38
 */
public class DynamicDatasourceContextHolder {

    private static final ThreadLocal<String> CONTEXT_HOLDER = new  ThreadLocal<String>();

    /**
     * 数据源列表
     */
    public static List<Object> datasourceIds = new ArrayList<>();


    /**
     * 设置数据源
     *
     * @param datasourceId
     */
    public static void setDataSourceType(String datasourceId) {
        CONTEXT_HOLDER.set(datasourceId);
    }



    /**
     * 获取当前数据源的id
     *
     * @return data source key
     */
    public static String getDataSourceType() {
        return CONTEXT_HOLDER.get();
    }

    /**
     * To set DataSource as default
     */
    public static void clearDataSourceType() {
        CONTEXT_HOLDER.remove();
    }

    /**
     * 判断当前列表中是否有指定的数据源
     *
     * @param datasourceId
     * @return Boolean
     */
    public static boolean containDataSourceType(String datasourceId) {
        return datasourceIds.contains(datasourceId);
    }
}
